<?php


namespace core\service;


use core\lib\Cache;
use core\model\SysMod;

class ModService {

    const CACHE_ENABLE_MODS = 'sys_enabled_mods';

    /**
     * 获得当前激活的Mod列表
     * @return array
     */
    public function getEnableMod(): array {
        $mods = Cache::sMembers(self::CACHE_ENABLE_MODS);
        if (empty($mods)) {
            $mods = (new SysMod())->getEnabledMods()->column('name');
            if (empty($mods)) {
                return [];
            }
            Cache::sAdd(self::CACHE_ENABLE_MODS, $mods);
        }
        return $mods;
    }

    /**
     * 销毁模组缓存
     * @return int
     */
    public function destroyModCache() {
        return Cache::delete(self::CACHE_ENABLE_MODS);
    }

    /**
     * 添加新模组
     * @param string $mod
     * @param string $alias
     */
    public function addMod(string $mod, string $alias = '') {
        (new SysMod())->insert([
            'name' => $mod,
            'alias' => $alias,
            'status' => SysMod::STATUS_DISABLED,
        ]);
    }

}
